<template>
  <div class="modifyBirth">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="submit"
    />
  </div>
</template>

<script>
import {editUserInfoAPI} from '@/api'

import dayjs from 'dayjs';
export default {
  props:{
    value: {
      type: String,
      required: true
    }
  },
  data(){
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    };
  },
  methods:{
    submit(val){
      let birth = dayjs(val).format('YYYY-MM-DD')
      this.editUserInfo(birth)
    },
    async editUserInfo(birth){
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '保存中...',
      })
      try {
        await editUserInfoAPI({
          birthday: birth
        })
        this.$emit('input',birth)
        this.$emit('close')
        this.$toast.success('保存成功')
      } catch (error) {
        this.$toast.fail('操作失败，请稍后重试！')
      }
  },
  }
}
</script>

<style>

</style>